package com.erpoa.controller;

import com.alibaba.fastjson.JSON;
import com.erpoa.annotation.NoRepeatSubmit;
import com.erpoa.dto.DealMessage;
import com.erpoa.entity.PackMessage;
import com.erpoa.service.WareHouseMaterialService;
import com.erpoa.utils.PDAException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.CannotSerializeTransactionException;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenjimin
 * @Title: WareHouseMaterialController
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2020/9/159:39
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "WareHouseMaterialController", tags = {"仓库发料"})
@RestController
@RequestMapping("/PDAService")
public class WareHouseMaterialController {

    private static final Logger logger = LoggerFactory.getLogger(WareHouseMaterialController.class);

    @Autowired
    private WareHouseMaterialService wareHouseMaterialService;

    @ApiOperation("仓库发料")
    @RequestMapping(value = {"/wareHouseMaterialServlet"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @NoRepeatSubmit(key = "wareHouseMaterialServlet:arg[0]")
    public DealMessage wareHouseMaterialServlet(@RequestBody PackMessage packMessage) {
        DealMessage dealMessage = new DealMessage(true);
        int optType = packMessage.getOptType();
        try {
            logger.debug("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());
            System.out.println("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());

            switch (optType) {
                case 0:
                    // 根据机台获取领退料信息
                    dealMessage.setMessage(JSON.toJSONString(wareHouseMaterialService.getMaterialList(packMessage)));
                    break;
                case 1:
                    // 扫描机台或者条码
                    dealMessage.setMessage(JSON.toJSONString(wareHouseMaterialService.queryMachineCodeOrBarCode(packMessage)));
                    break;
                case 2:
                    // 是否需要跳转物料交接
                    dealMessage.setMessage(wareHouseMaterialService.materialBusiness(packMessage));
                    break;
                case 4:
                    // 提交仓库发料
                    wareHouseMaterialService.jumpMaterialHandoverFragment(packMessage);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof PDAException) {
                dealMessage = new DealMessage(false, e.getMessage());
            } else if (e instanceof CannotSerializeTransactionException || e instanceof CannotCreateTransactionException) {
                dealMessage = new DealMessage(false, "数据正在处理中，请再试一次！");

            } else if (e instanceof TooManyResultsException) {
                dealMessage = new DealMessage(false, "数据错误，请检查！" + e.getMessage());

            } else {
                dealMessage = new DealMessage(false, "服务器hj接口出现未知错误，请再扫一次确认结果，如多次出现请及时反馈IT，分机号：6257！时间点：" + System.currentTimeMillis() + "错误信息：" + e.getMessage());
            }
            logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            System.out.println("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            return dealMessage;
        }
        logger.debug(dealMessage.toString());
        logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        System.out.println("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        return dealMessage;
    }


}
